# -*- coding: utf-8 -*-
"""
Created on Wed Dec 25 15:53:59 2019

@author: MS
"""

from sklearn import svm
import numpy as np
import matplotlib.pyplot as plt

#========西瓜数据集3.0α============
X=np.array([[0.697,0.46],[0.774,0.376],[0.634,0.264],[0.608,0.318],[0.556,0.215],
   [0.403,0.237],[0.481,0.149],[0.437,0.211],[0.666,0.091],[0.243,0.267],
   [0.245,0.057],[0.343,0.099],[0.639,0.161],[0.657,0.198],[0.36,0.37],
   [0.593,0.042],[0.719,0.103]])
Y=np.array([1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0])

#西瓜数据集3.0α中的密度和含糖量数据
x=X[:,0].reshape(-1, 1)
y=X[:,1]

#人为生成数据（若要计算西瓜数据集，可以将下面两行注释掉）
x=np.linspace(0,1,20).reshape(-1, 1)
y=np.sin(x*3.14)+np.random.randn(20,1)*0.1

#分别采用线性核和高斯核来拟合
reg1=svm.SVR(C=30,kernel='linear',epsilon=0.01).fit(x,y)
reg2=svm.SVR(C=30,kernel='rbf',epsilon=0.01).fit(x,y)

#画图
px=np.linspace(min(x),max(x),20).reshape(-1, 1)
plt.scatter(x,y,c='r',label='data')
plt.plot(px,reg1.predict(px),'g--',label='linear')
plt.plot(px,reg2.predict(px),'b--',label='rbf')
plt.legend()
plt.title('SVR result\n C=30,epsilon=0.01')
plt.xlabel('rou')
plt.ylabel('Sugar ratio')